package com.ly.hotel.spainterface.nm;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.MyLogsManager;
import com.ly.hotel.msgevent.SendLogEvent;
import com.ly.hotel.spainterface.nm.bean.NmXFBuyItemBean;
import com.ly.hotel.spainterface.nm.event.XFBuyReqEvent;
import com.ly.hotel.spainterface.nm.event.XFBuyResEvent;
import com.ly.hotel.spainterface.nm.event.XFResCardEvent;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by SHUBEN on 2018/1/26.
 */

public class XFBuyDialog extends Dialog {
    private NmXiaoFeiActivity mcontext;
    private List<NmXFBuyItemBean> buylist;
    private TextView totalText;
    private TextView operIntro;
    private ImageView operImg;
    private TextView closeText;
    private Button okBtn;
    private Button cancelBtn;
    private LinearLayout opeLayout;
    private int countTime = 9;


    public XFBuyDialog(NmXiaoFeiActivity context, int theme, List<NmXFBuyItemBean> bl) {
        super(context, theme);
        mcontext = context;
        buylist = bl;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setContentView(R.layout.nm_xf_buy_dialog);

        totalText = findViewById(R.id.xf_total_price);
        operIntro = findViewById(R.id.xf_oper_intro);
        operImg = findViewById(R.id.xf_oper_img);
        closeText = findViewById(R.id.xf_buy_suc_close);
        okBtn = findViewById(R.id.xf_buy_ok);
        cancelBtn = findViewById(R.id.xf_buy_cancel);
        opeLayout = findViewById(R.id.oper_layout);

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        initStep();
    }

    @Override
    protected void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    //收到手牌反馈
    public void onEventMainThread(XFResCardEvent e) {
        if(e.getType() == 1) {
            if (!e.getTag()){
                operIntro.setTextColor(mcontext.getResources().getColor(R.color.red));
                operIntro.setText(mcontext.getResources().getString(R.string.xf_buy_faild)+ e.getMsg());
                operImg.setImageResource(R.drawable.xf_buy_err);
                operImg.setVisibility(View.VISIBLE);
            }else{
                EventBus.getDefault().post(new XFBuyReqEvent(buylist,e.getMsg()));
            }
        }
    }

    //收到下单返回
    public void onEventMainThread(XFBuyResEvent e) {
        operImg.setVisibility(View.VISIBLE);
        if (!e.getTag()){
            operIntro.setTextColor(mcontext.getResources().getColor(R.color.red));
            operIntro.setText(mcontext.getResources().getString(R.string.xf_buy_faild)+
                    mcontext.getResources().getString(R.string.xf_call_waiter));
            operImg.setImageResource(R.drawable.xf_buy_err);

        }else{
            operIntro.setText(mcontext.getResources().getString(R.string.xf_buy_suc)+ "！"+
                    mcontext.getResources().getString(R.string.xf_buy_wait));
            operImg.setImageResource(R.drawable.xf_buy_suc);
            showCloseText();
            EventBus.getDefault().post(new SendLogEvent(MyLogsManager.CLOUD_LOGS_XIAOFEI + "\t" +
                    MyApplication.interfaceVersion + "\t" + "下单成功"));
        }
    }

    private void showCloseText(){
        closeText.setVisibility(View.VISIBLE);
        final Handler handler = new Handler(){
          public void handleMessage(Message msg){
              if(msg.what > 0){
                  closeText.setText(mcontext.getResources().getString(R.string.xf_window_close) + " " + msg.what
                          + " " + mcontext.getResources().getString(R.string.xf_time_unit));
              }else{
                  dismiss();
              }
          }
        };

        Runnable task = new Runnable() {
            @Override
            public void run() {
                if (countTime > 0) {
                    countTime--;
                }
                handler.sendEmptyMessage(countTime);
                handler.postDelayed(this, 1000);
            }
        };

        handler.postDelayed(task, 0);
    }

    //初始化数据和步骤
    private void initStep(){
        double totalPrice = 0.00;
        for(NmXFBuyItemBean ib : buylist){
            double price = ib.getPrice();
            double num = (double) ib.getNum();
            totalPrice += (price*num);
        }
        totalText.setText(mcontext.getResources().getString(R.string.xf_total_price) + String.format("%.2f",totalPrice) + " " + mcontext.getResources().getString(R.string.xf_rmb));
        operIntro.setText(mcontext.getResources().getString(R.string.xf_swipe_confire));
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                opeLayout.setVisibility(View.GONE);
                mcontext.buyGoods(buylist);
            }
        });
    }
}